package controller

import (
	"bytes"
	"fmt"
	"fx_swift_ifcgate_agent/api"
	"fx_swift_ifcgate_agent/internal/service"
	"fx_swift_ifcgate_agent/pkg/code"
	"fx_swift_ifcgate_agent/pkg/global"
	"fx_swift_ifcgate_agent/pkg/internalerrors"
	"fx_swift_ifcgate_agent/pkg/upload"
	"github.com/gin-gonic/gin"
	"github.com/xuri/excelize/v2"
	"net/http"
)

// 换汇明细列表
func ExchangeCurrencyList(c *gin.Context) {
	req := &api.ExchangeCurrencyListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewExchangeSrv().ExchangeCurrencyList(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

func ExportExchangeCurrency(c *gin.Context) {
	var (
		err      error
		filePath string
		// 获取最大导出数
		maxNum  = global.ViperObj.GetInt("export.maxNum")
		account = c.GetString("Account")
		line    = 1
	)

	req := &api.ExchangeCurrencyListReq{}
	if err = c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	req.PageSize = maxNum
	result, err := service.NewExchangeSrv().ExchangeCurrencyList(c, req)

	f := excelize.NewFile() // 设置单元格的值
	index, _ := f.NewSheet("Sheet1")
	// 这里设置表头
	f.SetCellValue("Sheet1", "A1", "编号")
	f.SetCellValue("Sheet1", "B1", "换汇企业名称(中文)")
	f.SetCellValue("Sheet1", "C1", "换汇企业名称(英文)")
	f.SetCellValue("Sheet1", "D1", "卖出币种")
	f.SetCellValue("Sheet1", "E1", "卖出金额")
	f.SetCellValue("Sheet1", "F1", "汇率")
	f.SetCellValue("Sheet1", "G1", "买入币种")
	f.SetCellValue("Sheet1", "H1", "买入金额")
	f.SetCellValue("Sheet1", "I1", "时间")
	f.SetCellValue("Sheet1", "J1", "状态")

	if len(result.List) > maxNum {
		code.Fail(c, http.StatusBadRequest, code.ErrCanNotOpenFile, "导出数据太多，请分批导出")
		return
	}
	// 循环写入数据
	for _, v := range result.List {
		line++
		f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.BizFlow)
		f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.MerNameCn)
		f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.MerNameEn)
		f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.SellCurrency)
		f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.SellAmount)
		f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.Rate)
		f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.BuyCurrency)
		f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.BuyAmount)
		f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.CreatedAt)
		f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.Status)
	}

	f.SetActiveSheet(index)

	var buffer bytes.Buffer
	_ = f.Write(&buffer)
	content := bytes.NewReader(buffer.Bytes())

	fileName := fmt.Sprintf("download/%s%s%s.xlsx", account, `-`, "换汇列表导出")
	if filePath, err = upload.UploadToS3Srv(fileName, content); err != nil || len(filePath) == 0 {
		code.Fail(c, http.StatusBadRequest, code.ErrCanNotOpenFile, err.Error())
		return
	}
	code.Success(c, filePath)
}
